package com.hk.Mgr.web.controller.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.UserCardReq;
import com.hk.Mgr.web.message.kyc.UserCardRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.ConstantCard;
import com.hk.commons.ConstantParm;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.enm.CardsTypeEnum;
import com.hk.commons.enm.UserCardsStatusEnum;
import com.hk.commons.entity.Card;
import com.hk.commons.entity.Parameter;
import com.hk.commons.entity.UserCard;
import com.hk.commons.entity.UserExtraInfo;
import com.hk.commons.vo.UserCardMsg;
import com.hk.commons.vo.app.CheckResultMsg;
import com.hk.service.IPointsFunction;
import com.hk.service.IUserExtraInfo;
import com.hk.service.kyc.ICard;
import com.hk.service.kyc.ICoupon;
import com.hk.service.kyc.IPoints;
import com.hk.service.kyc.IUserCard;
import com.hk.service.order.IParameter;
import com.hk.service.sys.ISysUser;

/**
 * 用户积分
 * 
 * @author jeff
 *
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/userCardCtr")
public class AppUserCardCtrl extends BaseCtrl {

	@Autowired
	private ISysUser userService;

	@Autowired
	private ICard cardService;

	@Autowired
	private IPoints pointsService;

	@Autowired
	private IUserCard userCardService;

	@Autowired
	private IPointsFunction PointsFunctionService;

	@Autowired
	private IUserExtraInfo userExtraService;
	
	@Autowired
	private ICoupon couponService;
	
	/**
	 * 获取加息券
	 * 
	 * @param userId
	 *            用户id, cardId 加息券id
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<UserCardRes>> list(@RequestBody UserCardReq req) {
		ResponseBase<List<UserCardRes>> resp = new ResponseBase<List<UserCardRes>>();
		List<UserCardRes> cardResList = new ArrayList<UserCardRes>();
		
		Long userId = req.getUserId();
		 
		
		List<UserCardMsg> couponResList =  new ArrayList<UserCardMsg>() ;
		
		if(req.getStatus() != null){
			Integer status = req.getStatus();
			couponResList = couponService.getCouponList(userId, status);
		}else{
			couponResList = couponService.getCouponList(userId);
		}
		
		if(couponResList != null && couponResList.size() > 0){
			for(UserCardMsg userCardMsg:couponResList){
				//只兼容使用加息券
				if(userCardMsg.getCardType() == ConstantCard.CARD_TYPE_JXQ){
					UserCardRes cardRes = new UserCardRes();
					cardRes.setId(userCardMsg.getUserCardId());
					cardRes.setCardRate(userCardMsg.getFaceValue().toString());
					cardRes.setCardType(userCardMsg.getCardType());
					cardRes.setCardTypeStr(userCardMsg.getCardTypeStr());
					cardRes.setCardDesc("0.5%加息卡");
					cardRes.setGetTimeStr(userCardMsg.getCardGetTime());
					// 添加卡券的状态信息
					cardRes.setStatus(userCardMsg.getStatus());
					cardRes.setStatusStr(userCardMsg.getStatusStr());
					cardRes.setExpireTimeStr(userCardMsg.getCardExpireTime()); 
					cardResList.add(cardRes);
					
					
				}
			}
		}
		
		resp.setSize(cardResList.size());
		resp.setResult(cardResList);
		resp.setResultCode(StatusCode.SUCCESS.key());
		return resp;
	}
	
	/**
	 * 获取加息券
	 * 
	 * @param userId
	 *            用户id, cardId 加息券id
	 * @return
	 */
	@RequestMapping(value = "/sendToUser", method = RequestMethod.GET)
	@ResponseBody
	public ResponseBase<Boolean> sendToUser() {
		ResponseBase<Boolean>  resp = new  ResponseBase<Boolean> ();
		
		HqlFilter hqlfilter = new HqlFilter();
		
		hqlfilter.addEQFilter("status", 0);
		
		List<UserCard> userCardList = userCardService.findByFilter(hqlfilter);
		
		int i = 0;
		for(UserCard uc:userCardList){
			couponService.sendCoupon(uc.getUserId(), Long.valueOf("374938122134262690"));
			i++;
		}
		
		resp.setSize(i);
		resp.setResult(true);
		resp.setResultCode(StatusCode.SUCCESS.key());
		return resp;
	}
	
}
